﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace Umbrella.Whs
{
    public static class UmbrellaHelpers
    {
        // http://dotnet-snippets.de/dns/create-nice-file-size-in-c-SID906.aspx
        public static string GetNiceFileSize(long size)
        {
            int run = 0;
            double d = Convert.ToDouble(size);

            string[] sizes = { "B", "KB", "MB", "GB" };

            while ((d >= 1024.0) && (run < sizes.Length - 1))
            {
                d /= 1024.0;
                run++;
            }

            double dou = Math.Round(d, 2);
            string sizestring = dou.ToString();

            return (sizestring + " " + sizes[run]);
        }

        public static string GetNiceDataRate(long rate)
        {
            int run = 0;
            double d = Convert.ToDouble(rate);

            string[] sizes = { "b", "Kb", "Mb", "Gb" };

            while ((d >= 1024.0) && (run < sizes.Length - 1))
            {
                d /= 1024.0;
                run++;
            }

            double dou = Math.Round(d, 2);
            string sizestring = dou.ToString();

            return (sizestring + " " + sizes[run] + "/s");
        }

        private readonly static SHA1CryptoServiceProvider _sha1 = new SHA1CryptoServiceProvider();

        /// <summary>
        /// calculates an id from unique data
        /// 
        /// same data will always result in the same id.
        /// different data will always result in different ids.
        /// </summary>
        public static string CreateId(params string[] data)
        {
            var data2 = string.Join("\n", data);

            var sha1 = _sha1.ComputeHash(Encoding.UTF8.GetBytes(data2));

            return BitConverter.ToString(sha1).Replace("-", string.Empty).ToLower();
        }
    }
}